﻿import {
  Component,
  Injector,
  ViewChild,
  ViewEncapsulation,
  AfterViewInit,
} from "@angular/core";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
  PipelineServiceProxy,
  PipelineListDto,
  PipelineNumDto,
} from "@shared/service-proxies/service-proxies";
import { LazyLoadEvent, Paginator } from "primeng/primeng";
import { Table } from "primeng/table";

@Component({
  templateUrl: "./step.component.html",
  styleUrls: ["./step.component.less"],
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()],
})
export class StepsComponent extends AppComponentBase implements AfterViewInit {
  @ViewChild("paginator") paginator: Paginator;
  @ViewChild("dataTable") dataTable: Table;
  counts = new PipelineNumDto();
  selectedpipe:Array<string>;
  filterText = "";
  isFinished = false;
  isMy = true;
  type = 1;
  constructor(
    injector: Injector,
    private _pipelineService: PipelineServiceProxy
  ) {
    super(injector);
  }

  ngAfterViewInit(): void {
    this._pipelineService.getNum().subscribe((result) => {
      this.counts = result;
    });

    this.getPipes();
  }

  getMyDoingPipeines() {
    if (!this.isFinished && this.isMy) {
      return;
    }

    this.isFinished = false;
    this.isMy = true;
    this.type = 1;
    this.getPipes();
  }

  getOtherDoingPipeines() {
    if (!this.isFinished && !this.isMy) {
      return;
    }

    this.isFinished = false;
    this.isMy = false;
    this.type = 2;
    this.getPipes();
  }

  getFilishedPipeines() {
    if (this.isFinished) {
      return;
    }

    this.isFinished = true;
    this.type = 3;
    this.getPipes();
  }

  getPipes(event?: LazyLoadEvent) {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);
      return;
    }

    this.primengTableHelper.showLoadingIndicator();

    this._pipelineService
      .getPipelines(
        this.filterText,
        this.type,
        "53",
        undefined,
        "",
        this.primengTableHelper.getSorting(this.dataTable),
        this.primengTableHelper.getMaxResultCount(this.paginator, event),
        this.primengTableHelper.getSkipCount(this.paginator, event)
      )
      .subscribe((result) => {
        console.log(result);
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.primengTableHelper.records = result.items;
        //console.log(this.primengTableHelper);
        this.primengTableHelper.hideLoadingIndicator();
      });
  }

  pipelineRun(pipe: PipelineListDto) {
    this._pipelineService.getPipelineAndRun(pipe.id).subscribe((result) => {
      this.notify.info("运行成功");
      this.getPipes();
    });
  }
}
